{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Fqp93JixVuiN"
      },
      "source": [
        "##### Copyright 2018 The TensorFlow Probability Authors.\n",
        "\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "MeKZo1dnV1cE"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\"); { display-mode: \"form\" }\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DcriL2xPrG3_"
      },
      "source": [
        "# TensorFlow Distributions: A Gentle Introduction\n",
        "\n",
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/probability/examples/TensorFlow_Distributions_Tutorial\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/TensorFlow_Distributions_Tutorial.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/TensorFlow_Distributions_Tutorial.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/probability/tensorflow_probability/examples/jupyter_notebooks/TensorFlow_Distributions_Tutorial.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "10i0wSQfJClb"
      },
      "source": [
        "In this notebook, we'll explore TensorFlow Distributions (TFD for short). The goal of this notebook is to get you gently up the learning curve, including understanding TFD's handling of tensor shapes. This notebook tries to present examples before rather than abstract concepts. We'll present canonical easy ways to do things first, and save the most general abstract view until the end. If you're the type who prefers a more abstract and reference-style tutorial, check out [Understanding TensorFlow Distributions Shapes](https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/Understanding_TensorFlow_Distributions_Shapes.ipynb). If you have any questions about the material here, don't hesitate to contact (or join) [the TensorFlow Probability mailing list](https://groups.google.com/a/tensorflow.org/forum/#!forum/tfprobability). We're happy to help."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kII2QSIEJn0X"
      },
      "source": [
        "Before we start, we need to import the appropriate libraries. Our overall library is `tensorflow_probability`. By convention, we generally refer to the distributions library as `tfd`.\n",
        "\n",
        "[Tensorflow Eager](https://www.tensorflow.org/guide/eager) is an imperative execution environment for TensorFlow. In TensorFlow eager, every TF operation is immediately evaluated and produces a result. This is in contrast to TensorFlow's standard \"graph\" mode, in which TF operations add nodes to a graph which is later executed. This entire notebook is written using TF Eager, although none of the concepts presented here rely on that, and TFP can be used in graph mode."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "J6t0EUihrG4B"
      },
      "outputs": [],
      "source": [
        "import collections\n",
        "\n",
        "import tensorflow as tf\n",
        "import tensorflow_probability as tfp\n",
        "tfd = tfp.distributions\n",
        "\n",
        "try:\n",
        "  tf.compat.v1.enable_eager_execution()\n",
        "except ValueError:\n",
        "  pass\n",
        "\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QD5lzFZerG4H"
      },
      "source": [
        "## Basic Univariate Distributions\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Gos16Z82LSQQ"
      },
      "source": [
        "Let's dive right in and create a normal distribution:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "r3zofkWpLEvY"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Normal 'Normal' batch_shape=[] event_shape=[] dtype=float32>"
            ]
          },
          "execution_count": 3,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n = tfd.Normal(loc=0., scale=1.)\n",
        "n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7rlXP5HaLVsc"
      },
      "source": [
        "We can draw a sample from it:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "14RRJONELX3O"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=0.25322816>"
            ]
          },
          "execution_count": 4,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.sample()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NsDAltf5Le33"
      },
      "source": [
        "We can draw multiple samples:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "v5jdTzbqLhrl"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-1.4658079, -0.5653636,  0.9314412], dtype=float32)>"
            ]
          },
          "execution_count": 5,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.sample(3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4wX5cjUXLmrD"
      },
      "source": [
        "We can evaluate a log prob:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "hrCTzv2cLoLw"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-0.9189385>"
            ]
          },
          "execution_count": 6,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.log_prob(0.)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lHYIb0psLrzE"
      },
      "source": [
        "We can evaluate multiple log probabilities:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "4dgwzazNLw6H"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.9189385, -2.9189386, -8.918939 ], dtype=float32)>"
            ]
          },
          "execution_count": 7,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.log_prob([0., 2., 4.])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mY5hHMClL-i1"
      },
      "source": [
        "We have a wide range of distributions. Let's try a Bernoulli:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "OIJErPQWMDfP"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Bernoulli 'Bernoulli' batch_shape=[] event_shape=[] dtype=int32>"
            ]
          },
          "execution_count": 8,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b = tfd.Bernoulli(probs=0.7)\n",
        "b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "oqDcgYE8Mck2"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=int32, numpy=1>"
            ]
          },
          "execution_count": 9,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "6HbbzPNTMgXh"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(8,), dtype=int32, numpy=array([1, 0, 0, 0, 1, 0, 1, 0], dtype=int32)>"
            ]
          },
          "execution_count": 10,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b.sample(8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "LNy0tIKmMuL3"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-0.35667497>"
            ]
          },
          "execution_count": 11,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b.log_prob(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "sghhA8onM0IN"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(4,), dtype=float32, numpy=array([-0.35667497, -1.2039728 , -0.35667497, -1.2039728 ], dtype=float32)>"
            ]
          },
          "execution_count": 12,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b.log_prob([1, 0, 1, 0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ztM2d-N9nNX2"
      },
      "source": [
        "## Multivariate Distributions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MT2ZyGCoHMla"
      },
      "source": [
        "We'll create a multivariate normal with a diagonal covariance:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "MuFrhR4enQI5"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.MultivariateNormalDiag 'MultivariateNormalDiag' batch_shape=[] event_shape=[2] dtype=float32>"
            ]
          },
          "execution_count": 13,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd = tfd.MultivariateNormalDiag(loc=[0., 10.], scale_diag=[1., 4.])\n",
        "nd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QUcnlm3vHFRj"
      },
      "source": [
        "Comparing this to the univariate normal we created earlier, what's different? "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "ggInhJ-LHVhR"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Normal 'Normal' batch_shape=[] event_shape=[] dtype=float32>"
            ]
          },
          "execution_count": 14,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "tfd.Normal(loc=0., scale=1.)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0ze8A19LnqO5"
      },
      "source": [
        "We see that the univariate normal has an `event_shape` of `()`, indicating it's a scalar distribution. The multivariate normal has an `event_shape` of `2`, indicating the basic [event space](https://en.wikipedia.org/wiki/Event_(probability_theory&#41;) of this distribution is two-dimensional."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lJTTGCuuHpf5"
      },
      "source": [
        "Sampling works just as before:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "xQzfj0vHnw-5"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(2,), dtype=float32, numpy=array([-1.2489667, 15.025171 ], dtype=float32)>"
            ]
          },
          "execution_count": 15,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "dSyxxVmNnzT1"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(5, 2), dtype=float32, numpy=\n",
              "array([[-1.5439653 ,  8.9968405 ],\n",
              "       [-0.38730723, 12.448896  ],\n",
              "       [-0.8697963 ,  9.330035  ],\n",
              "       [-1.2541095 , 10.268944  ],\n",
              "       [ 2.3475595 , 13.184147  ]], dtype=float32)>"
            ]
          },
          "execution_count": 16,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.sample(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "SC0fmV3hn0Zp"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-3.2241714>"
            ]
          },
          "execution_count": 17,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.log_prob([0., 10])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lmI_pjVJIT4I"
      },
      "source": [
        "Multivariate normals do not in general have diagonal covariance. TFD offers multiple ways to create multivariate normals, including a full-covariance specification, which we use here."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "qPEWjisBolk2"
      },
      "outputs": [
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJztnXtwVHWa/p/OjYSkc4Mk5AIqECBA\nQghR8DIKEkBXV1fRqRHc2VFcSktrVi2t1a0Zd2qmdscV3JFZd93KFmzhBbGc3VIX8QIoyCCC8cJF\nJISbkgSaQNK5kHvSvz+e3+F0ku5Op9OdTk6eT1WqSef0Od8+VD3f9zzf932/NpfL5YIQQogRT0S4\nByCEECI4SNCFEMIiSNCFEMIiSNCFEMIiSNCFEMIiSNCFEMIiSNCFEMIiSNDFiODKK69EXFwc7HY7\nkpOTcd111+E///M/0d3d7dfnT58+DZvNhs7OzhCPtCc7d+5ETk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FLtIiF6owjdAgzHKFOLjUIMPYrQLcJwizK12CjE0KMIXYSEkhLaPk4nt6lzOvl7\nKDanEEIQRegi6BjZLQ0NwA8/cLOI/PyBb1MnhBgYEnQRVNy3lysoMBdoh2PKohBWQ5aLCCr+bC8n\nhAgNEnQRVJTdIkT4kKCLoDIci5yEGC1I0EVQUXaLEOFDgi6CynAschJitKAsFxF0hluRkxCjBUXo\nQghhESToQghhESToQghhESToQghhESToQghhEQIW9DNnzmDRokXIy8vDrFmzsG7dumCOSwghxAAJ\nOG0xKioKL774IoqKitDY2Ih58+ZhyZIlmDlzZjDHJ4QQwk8CjtAzMzNRVFQEALDb7cjLy0NVuHcl\nFkKIUUxQCotOnz6Nb775BvPnz+/zt9LSUpSWlgIAampqgnE5IYQQHhj0omhTUxOWL1+Ol156CYm9\n2+wBWL16NcrKylBWVoa0tLTBXk4IIYQXBiXoHR0dWL58OVauXIm77747WGMSQggRAAELusvlwqpV\nq5CXl4cnn3wymGMSQggRAAEL+p49e/Daa6/hk08+QWFhIQoLC7F169Zgjk0IIcQACHhR9IYbboDL\n5QrmWIQQQgwCVYoKIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRF\nkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKAL\nIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRF\nkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFkKALIYRFGJSgf/jhh5g+\nfTqmTp2K559/PlhjEkIIEQABC3pXVxceffRRfPDBBzhy5AjefPNNHDlyJJhjE0IIMQACFvT9+/dj\n6tSpmDx5MmJiYvCzn/0M7777bjDHJoQQYgBEBfrBqqoqTJw48fLvOTk52LdvX5/jSktLUVpaCgA4\nevQoiouLA71kUKipqUFaWlpYxzBc0L0w0b0w0b0wGS734vTp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2GRl5eH\nn/70p6NSzAFGpa+99ho++eQTFBYWorCwEFu3bg33sMQAGPURuhBCWAVF6EIIYREk6EIIYREk6EII\nYREk6EIIYREk6EIIYREk6EIIYREk6EIIYRH+H7SxFvfF0HJDAAAAAElFTkSuQmCC\n",
            "text/plain": [
              "<Figure size 600x400 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "nd = tfd.MultivariateNormalFullCovariance(\n",
        "    loc = [0., 5], covariance_matrix = [[1., .7], [.7, 1.]])\n",
        "data = nd.sample(200)\n",
        "plt.scatter(data[:, 0], data[:, 1], color='blue', alpha=0.4)\n",
        "plt.axis([-5, 5, 0, 10])\n",
        "plt.title(\"Data set\")\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "57lLzC7MQV-9"
      },
      "source": [
        "## Multiple Distributions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aRYY7-KvQupB"
      },
      "source": [
        "Our first Bernoulli distribution represented a flip of a single fair coin. We can also create a batch of independent Bernoulli distributions, each with their own parameters, in a single `Distribution` object:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "as9fo-XtRAFo"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Bernoulli 'Bernoulli' batch_shape=[3] event_shape=[] dtype=int32>"
            ]
          },
          "execution_count": 19,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3 = tfd.Bernoulli(probs=[.3, .5, .7])\n",
        "b3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "x_7t57XzRGVD"
      },
      "source": [
        "It's important to be clear on what this means. The above call defines three independent Bernoulli distributions, which happen to be contained in the same Python `Distribution` object. The three distributions cannot be manipulated individually. Note how the `batch_shape` is `(3,)`, indicating a batch of three distributions, and the `event_shape` is `()`, indicating the individual distributions have a univariate event space.\n",
        "\n",
        "\n",
        "If we call `sample`, we get a sample from all three:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "bQQJ_N7XRkuh"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=int32, numpy=array([0, 1, 1], dtype=int32)>"
            ]
          },
          "execution_count": 20,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3.sample()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "id": "aM6JOl3HSQb3"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(6, 3), dtype=int32, numpy=\n",
              "array([[1, 0, 1],\n",
              "       [0, 1, 1],\n",
              "       [0, 0, 1],\n",
              "       [0, 0, 1],\n",
              "       [0, 0, 1],\n",
              "       [0, 1, 0]], dtype=int32)>"
            ]
          },
          "execution_count": 21,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3.sample(6)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7NRbaUyLR2yf"
      },
      "source": [
        "If we call  `prob`, (this has the same shape semantics as `log_prob`; we use `prob` with these small Bernoulli examples for clarity, although `log_prob` is usually preferred in applications) we can pass it a vector and evaluate the probability of each coin yielding that value:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "id": "UKRV_z47NUV9"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.29999998, 0.5       , 0.29999998], dtype=float32)>"
            ]
          },
          "execution_count": 22,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3.prob([1, 1, 0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y3MexqrtREPP"
      },
      "source": [
        "Why does the API include batch shape? Semantically, one could perform the same computations by creating a list of distributions and iterating over them with a `for` loop (at least in Eager mode, in TF graph mode you'd need a `tf.while` loop). However, having a (potentially large) set of identically parameterized distributions is extremely common, and the use of vectorized computations whenever possible is a key ingredient in being able to perform fast computations using hardware accelerators."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "t52ptQXvUO07"
      },
      "source": [
        "## Using Independent To Aggregate Batches to Events"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oN3mut1NTOXX"
      },
      "source": [
        "In the previous section, we created `b3`, a single `Distribution` object that represented three coin flips. If we called `b3.prob` on a vector $v$, the $i$'th entry was the probability that the $i$th coin takes value $v[i]$.\n",
        "\n",
        "Suppose we'd instead like to specify a \"joint\" distribution over independent random variables from the same underlying family. This is a different object mathematically, in that for this new distribution, `prob` on a vector $v$ will return a single value representing the probability that the entire set of coins matches the vector $v$.\n",
        "\n",
        "How do we accomplish this? We use a \"higher-order\" distribution called `Independent`, which takes a distribution and yields a new distribution with the batch shape moved to the event shape:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "id": "V_DcGAG2Tqxj"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Independent 'IndependentBernoulli' batch_shape=[] event_shape=[3] dtype=int32>"
            ]
          },
          "execution_count": 23,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3_joint = tfd.Independent(b3, reinterpreted_batch_ndims=1)\n",
        "b3_joint"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Zkv5TRVFVLUo"
      },
      "source": [
        "Compare the shape to that of the original `b3`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {
        "id": "5bBsLX-6VT36"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Bernoulli 'Bernoulli' batch_shape=[3] event_shape=[] dtype=int32>"
            ]
          },
          "execution_count": 24,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0uveNoPNVVYy"
      },
      "source": [
        "As promised, we see that that `Independent` has moved the batch shape into the event shape: `b3_joint` is a single distribution (`batch_shape = ()`) over a three-dimensional event space (`event_shape = (3,)`).\n",
        "\n",
        "Let's check the semantics:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "id": "eDsO2gLcVlY9"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=0.044999998>"
            ]
          },
          "execution_count": 25,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3_joint.prob([1, 1, 0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IktKInQ5WQJz"
      },
      "source": [
        "An alternate way to get the same result would be to compute probabilities using `b3` and do the reduction manually by multiplying (or, in the more usual case where log probabilities are used, summing):"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "id": "rRIVEchSV-RZ"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=0.044999994>"
            ]
          },
          "execution_count": 26,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "tf.reduce_prod(b3.prob([1, 1, 0]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ikayH3d2Wcf-"
      },
      "source": [
        "`Indpendent` allows the user to more explicitly represent the desired concept. We view this as extremely useful, although it's not strictly necessary."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wVivnv1qWi9f"
      },
      "source": [
        "Fun facts:\n",
        "\n",
        "\n",
        "*   `b3.sample` and `b3_joint.sample` have different conceptual implementations, but indistinguishable outputs: the difference between a batch of independent distributions and a single distribution created from the batch using `Independent` shows up when computing probabilites, not when sampling.\n",
        "*   `MultivariateNormalDiag` could be trivially implemented using the scalar `Normal` and `Independent` distributions (it isn't actually implemented this way, but it could be).\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "INu1viAVXz93"
      },
      "source": [
        "## Batches of Multivariate Distirbutions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "G_cEhLU-Tjhm"
      },
      "source": [
        "Let's create a batch of three full-covariance two-dimensional multivariate normals: "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "id": "mtxwqizfTwKi"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.MultivariateNormalFullCovariance 'MultivariateNormalFullCovariance' batch_shape=[3] event_shape=[2] dtype=float32>"
            ]
          },
          "execution_count": 27,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd_batch = tfd.MultivariateNormalFullCovariance(\n",
        "    loc = [[0., 0.], [1., 1.], [2., 2.]],\n",
        "    covariance_matrix = [[[1., .1], [.1, 1.]], \n",
        "                         [[1., .3], [.3, 1.]],\n",
        "                         [[1., .5], [.5, 1.]]])\n",
        "nd_batch"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "osDjz1vXUVkr"
      },
      "source": [
        "We see `batch_shape = (3,)`, so there are three independent multivariate normals, and `event_shape =  (2,)`, so each multivariate normal is two-dimensional. In this example, the individual distributions do not have independent elements.\n",
        "\n",
        "Sampling works:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {
        "id": "82u32RUpYKeK"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(4, 3, 2), dtype=float32, numpy=\n",
              "array([[[ 0.7367498 ,  2.730996  ],\n",
              "        [-0.74080074, -0.36466932],\n",
              "        [ 0.6516018 ,  0.9391426 ]],\n",
              "\n",
              "       [[ 1.038303  ,  0.12231752],\n",
              "        [-0.94788766, -1.204232  ],\n",
              "        [ 4.059758  ,  3.035752  ]],\n",
              "\n",
              "       [[ 0.56903946, -0.06875849],\n",
              "        [-0.35127294,  0.5311631 ],\n",
              "        [ 3.4635801 ,  4.565582  ]],\n",
              "\n",
              "       [[-0.15989424, -0.25715637],\n",
              "        [ 0.87479895,  0.97391707],\n",
              "        [ 0.5211419 ,  2.32108   ]]], dtype=float32)>"
            ]
          },
          "execution_count": 28,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd_batch.sample(4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2I-cYckNYTmf"
      },
      "source": [
        "Since `batch_shape = (3,)` and `event_shape = (2,)`, we pass a tensor of shape `(3, 2)` to `log_prob`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "id": "-7p02_66YRpX"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-1.8328519, -1.7907217, -1.694036 ], dtype=float32)>"
            ]
          },
          "execution_count": 29,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd_batch.log_prob([[0., 0.], [1., 1.], [2., 2.]])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "72uiME85SmEH"
      },
      "source": [
        "## Broadcasting, aka Why Is This So Confusing?"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3aWnXjyYZYtp"
      },
      "source": [
        "Abstracting out what we've done so far, every distribution has an batch shape `B` and an event shape `E`. Let `BE` be the concatenation of the event shapes:\n",
        "\n",
        "\n",
        "* For the univariate scalar distributions `n` and `b`, `BE = ().`.\n",
        "* For the two-dimensional multivariate normals `nd`. `BE = (2).`\n",
        "* For both `b3` and `b3_joint`, `BE = (3).`\n",
        "* For the batch of multivariate normals `ndb`, `BE = (3, 2).`\n",
        "\n",
        "The \"evaluation rules\" we've been using so far are:\n",
        "\n",
        "*   Sample with no argument returns a tensor with shape `BE`; sampling with a scalar n returns an \"n by `BE`\" tensor.\n",
        "*   `prob` and `log_prob` take a tensor of shape `BE` and return a result of shape `B`.\n",
        "\n",
        "The actual \"evaluation rule\" for `prob` and `log_prob` is more complicated, in a way that offers potential power and speed but also complexity and challenges.  The actual rule is (essentially) that **the argument to `log_prob` *must*  be [broadcastable](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) against `BE`; any \"extra\" dimensions are preserved in the output.** "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iwv81UjpmlkX"
      },
      "source": [
        "Let's explore the implications. For the univariate normal `n`, `BE = ()`, so `log_prob` expects a scalar. If we pass `log_prob` a tensor with non-empty shape, those show up as batch dimensions in the output:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "metadata": {
        "id": "xRMkZd2cnqnG"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.Normal 'Normal' batch_shape=[] event_shape=[] dtype=float32>"
            ]
          },
          "execution_count": 30,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n = tfd.Normal(loc=0., scale=1.)\n",
        "n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "metadata": {
        "id": "mci1cs1NnLDb"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-0.9189385>"
            ]
          },
          "execution_count": 31,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.log_prob(0.)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {
        "id": "MQW1XSB1nRlH"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(1,), dtype=float32, numpy=array([-0.9189385], dtype=float32)>"
            ]
          },
          "execution_count": 32,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.log_prob([0.])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 33,
      "metadata": {
        "id": "z-6d3PtTnT1W"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n",
              "array([[-0.9189385, -1.4189385],\n",
              "       [-1.4189385, -2.9189386]], dtype=float32)>"
            ]
          },
          "execution_count": 33,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n.log_prob([[0., 1.], [-1., 2.]])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6BkE19lEh9XY"
      },
      "source": [
        "Let's turn to the two-dimensional multivariate normal `nd` (parameters changed for illustrative purposes):"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "metadata": {
        "id": "Y1D3zg9kn8HJ"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tfp.distributions.MultivariateNormalDiag 'MultivariateNormalDiag' batch_shape=[] event_shape=[2] dtype=float32>"
            ]
          },
          "execution_count": 34,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd = tfd.MultivariateNormalDiag(loc=[0., 1.], scale_diag=[1., 1.])\n",
        "nd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SyZS-on4oCR4"
      },
      "source": [
        "`log_prob` \"expects\" an argument with shape `(2,)`, but it will accept any argument that broadcasts against this shape: "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "metadata": {
        "id": "RHyn5rV7oMzq"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-2.337877>"
            ]
          },
          "execution_count": 35,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.log_prob([0., 0.])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DTnAETFGo17O"
      },
      "source": [
        "But we can pass in \"more\" examples, and evaluate all their `log_prob`'s at once:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "metadata": {
        "id": "-eSm6Hnlo1sn"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-2.337877 , -2.337877 , -4.3378773], dtype=float32)>"
            ]
          },
          "execution_count": 36,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.log_prob([[0., 0.],\n",
        "             [1., 1.],\n",
        "             [2., 2.]])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dgxneFROpG7L"
      },
      "source": [
        "Perhaps less appealingly, we can broadcast over the event dimensions:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 37,
      "metadata": {
        "id": "-YRxLZLcoW29"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=-2.337877>"
            ]
          },
          "execution_count": 37,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.log_prob([0.])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 38,
      "metadata": {
        "id": "Md6RkXrcpNiK"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-2.337877 , -2.337877 , -4.3378773], dtype=float32)>"
            ]
          },
          "execution_count": 38,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "nd.log_prob([[0.], [1.], [2.]])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "266h1o2KoZZL"
      },
      "source": [
        "Broadcasting this way is a consequence of our \"enable broadcasting whenever possible\" design; this usage is somewhat controversial and could potentially be removed in a future version of TFP.\n",
        "\n",
        "Now let's look at the three coins example again:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mKHtmSP6SnvY"
      },
      "outputs": [],
      "source": [
        "b3 = tfd.Bernoulli(probs=[.3, .5, .7])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bGOJAgv_p059"
      },
      "source": [
        "Here, using broadcasting to represent the probability that *each* coin comes up heads is quite intuitive:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 40,
      "metadata": {
        "id": "ZYC6J8-dp50r"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.29999998, 0.5       , 0.7       ], dtype=float32)>"
            ]
          },
          "execution_count": 40,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3.prob([1])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5gxdAEjBiLiw"
      },
      "source": [
        "(Compare this to `b3.prob([1., 1., 1.])`, which we would have used back where `b3` was introduced.)\n",
        "\n",
        "Now suppose we want to know, for each coin, the probability the coin comes up heads *and* the probability it comes up tails. We could imagine trying:\n",
        "\n",
        "`b3.log_prob([0, 1])`\n",
        "\n",
        "Unfortunately, this produces an error with a long and not-very-readable stack trace. `b3` has `BE = (3)`, so we must pass `b3.prob` something broadcastable against `(3,)`. `[0, 1]` has shape `(2)`, so it doesn't broadcast and creates an error.  Instead, we have to say:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 41,
      "metadata": {
        "id": "_ry9LMiIieUx"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(2, 3), dtype=float32, numpy=\n",
              "array([[0.7, 0.5, 0.3],\n",
              "       [0.3, 0.5, 0.7]], dtype=float32)>"
            ]
          },
          "execution_count": 41,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ],
      "source": [
        "b3.prob([[0], [1]])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mxZ1WeK1qRcc"
      },
      "source": [
        "Why? `[[0], [1]]` has shape `(2, 1)`,  so it broadcasts against shape `(3)` to make a broadcast shape of `(2, 3)`."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WJBxD-zOrLDQ"
      },
      "source": [
        "Broadcasting is quite powerful: there are cases where it allows order-of-magnitude reduction in the amount of memory used, and it often makes user code shorter. However, it can be challenging to program with. If you call `log_prob` and get an error, a failure to broadcast is nearly always the problem."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JpjjIGThrj8Q"
      },
      "source": [
        "## Going Farther\n",
        "\n",
        "In this tutorial, we've (hopefully) provided a simple introduction. A few pointers for going further:\n",
        "\n",
        "*   `event_shape`, `batch_shape` and `sample_shape` can be arbitrary rank (in this tutorial they are always either scalar or rank 1). This increases the power but again can lead to programming challenges, especially when broadcasting is involved. For an additional deep dive into shape manipulation, see the [Understanding TensorFlow Distributions Shapes](https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/Understanding_TensorFlow_Distributions_Shapes.ipynb). \n",
        "*   TFP includes a powerful abstraction known as `Bijectors`, which in conjunction with `TransformedDistribution`, yields a flexible, compositional way to easily create new distributions that are invertible transformations of existing distributions. We'll try to write a tutorial on this soon, but in the meantime, check out [the documentation](https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/TransformedDistribution)\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "TensorFlow_Distributions_Tutorial.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
